<?php

namespace App\Http\Controllers\Admin;
use App\Model\Admin;
use App\Model\Cate;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
// 引入验证码类Captcha
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
// 引入验证规则
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    // 后台登录页面
    public function login()
    {
        return view('admin.login');
    }
    //验证码
    public function VCode()
    {
        //生成验证码图片的Builder对象，配置相应属性
        $phrase = (new PhraseBuilder())->build(4,'abcdefghijklmnopqrstuvwxyz1234567890');
        $builder = new CaptchaBuilder($phrase);
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 50, $font = null);
        //获取验证码的文本内容
        $phrase = $builder->getPhrase();
        //把内容存入session
        session()->put('imgcode', $phrase);
        //生成图片
        return $builder->output();
    }

    // 验证规则
    public function doLogin(Request $request)
    {

        // 获取参数
        $input = $request->except('_token');
        // 验证规则
        $rule = [
            'username' => 'required|between:4,18',
            'password' => 'required|between:4,18|alpha_dash',
        ];

        $message = [
            'username.required' => '用户名必须填写',
            'username.between' => '用户名长度必须是4-18位',
            'password.required' => '密码必须填写',
            'password.between' => '密码长度必须是4-18位',
            'password.alpha_dash' => '密码必须是数字字母下划线',
        ];

        $validator = Validator::make($input,$rule,$message);

        if($validator->fails()){
//            $errors = $validator->errors()->first();
            return redirect('admin/login')->withErrors($validator) ->withInput();
        }
        // 验证码
        if($input['imgcode'] != session()->get('imgcode')){
            return redirect('admin/login')->withErrors('验证码错误','imgcode') ->withInput();
        }

        // 验证
        $admin = Admin::where('username',$input['username'])->first();

        if(!$admin)
        {
            return redirect('admin/login')->withErrors('管理员不存在','admin') ->withInput();
        }

        $password = md5($input['password'].$admin['salt']);

        if($password != $admin['password'])
        {
            return redirect('admin/login')->withErrors('密码错误','password') ->withInput();
        }

        // 判断管理员的状态
        if($admin['state'] != 1)
        {
            session()->flush();
            return redirect('admin/login')->withErrors('管理员已禁用','admin') ->withInput();
        }

        // 保存admin
        session()->put('LoginAdmin',md5($admin['id'].$admin['salt']));
        session()->put('AdminNickName',$admin['nickname']);

        // 登录成功
        return redirect('admin/index');

    }

    // 退出登录
    public function logout()
    {
        // 清空session
        session()->flush();
        // 重定向
        return redirect('admin/login');
    }

    // 没有权限路由，跳转对应页面
    public function noaccess()
    {
        return view('admin.errors.noaccess');
    }
    // 加密接口
    public function jiami()
    {
        // 1.md5加密方式
        $str = '123456';
//        md5();
//        $str_md5 = md5($str);
//        return $str_md5;
        // hash加密
//        $str_hash = Hash::make($str);
        // 2.用户登录密码123456，mysql的hash字段先解密
        //        Hash::make('加密字符串'); 作比较 Hash::check('传递的字符串','mysql的Hash数据');
//        $mysqlhash = '$2y$10$i9y/D5q7Pcv2ExWn1zhl7eO9KT2jYPim651pNe4gtlcM.udr89bBW';
//        $str_hash = Hash::check($str,$mysqlhash);
//        if($str == $str_hash){
//            return '密码正确';
//        }else{
//            return '密码错误';
//        }

        // 3.Crypt
//        $str_Crypt = Crypt::encrypt($str);
//        return $str_Crypt;

//        Crypt::encrypt('加密字符串'); 作比较 Crypt::decrypt('解密mysql的Crypt字符串');
    }

    // Cache缓存
    public function Cache()
    {
//        $cate = Cate::find(1);
//
//        Cache::put('cate_1',$cate,20);

//        $cate = Cate::get()->toArray();

//        Cache::putmany($cate,20);

        return Cache::remember('category', 60, function () {

            return Cate::all();

        });
    }

    public function getCache()
    {
        $cate = Cate::all();
//      $cate = Cache::get('cate_1');
//        $keys = [
//            'cate_1'
//        ];
//        $cate = Cache::many($keys);

        return $cate;
    }

}
